﻿Imports DotNetNuke.Entities.Modules
Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports Aricie.DNN.UI.Controls

Public Class LuceneSearchUpgradeControl
    Inherits AriciePortalModuleBase

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim DMC As New DesktopModuleController()
        Dim DMI = DMC.GetDesktopModuleByModuleName(Aricie.DNN.Modules.LuceneSearch.Common.Constants.ModuleNameLuceneSearchResults)

        lblCurrent.Text = DMI.Version.ToString()
        lblCurrent2.Text = DMI.Version.ToString()
        lblBase.Text = Global.UpgradeSystem.VersionManagement.InstalledVersion.CurrentlyInstalledVersion.Version.ToString()
        lblBase2.Text = Global.UpgradeSystem.VersionManagement.InstalledVersion.CurrentlyInstalledVersion.Version.ToString()

        If Global.UpgradeSystem.VersionManagement.InstalledVersion.CurrentlyInstalledVersion.Version = "0.0.0" Then
            pnlNoPreviousVersion.Visible = True
            pnlPreviousVersion.Visible = False
        Else
            pnlNoPreviousVersion.Visible = False
            pnlPreviousVersion.Visible = True
        End If

        'Global.UpgradeSystem.VersionManagement.InstalledVersion.CurrentlyInstalledVersion.Version
    End Sub

    Private Sub btnUpgrade_Click(sender As Object, e As System.EventArgs) Handles btnUpgrade.Click
        Dim DMC As New DesktopModuleController()
        Dim DMI = DMC.GetDesktopModuleByModuleName(Aricie.DNN.Modules.LuceneSearch.Common.Constants.ModuleNameLuceneSearchResults)

        Dim UE As New Aricie.DNN.Services.UpgradeSystem.UpgradeEngine(Of LuceneSearchController)(Aricie.DNN.Modules.LuceneSearch.Common.Constants.ModuleNameLuceneSearchResults)
        UE.ResolveAndAddUpgrades()
        ' déterminer la version courante
        UE.MigrateSettings(New Version(Global.UpgradeSystem.VersionManagement.InstalledVersion.CurrentlyInstalledVersion.Version), New Version(DMI.Version))

        WriteNewValueAndRedirect()
    End Sub

    Private Sub btnUpgradeFrom_Click(sender As Object, e As System.EventArgs) Handles btnUpgradeFrom.Click
        Dim DMC As New DesktopModuleController()
        Dim DMI = DMC.GetDesktopModuleByModuleName(Aricie.DNN.Modules.LuceneSearch.Common.Constants.ModuleNameLuceneSearchResults)

        Dim UE As New Aricie.DNN.Services.UpgradeSystem.UpgradeEngine(Of LuceneSearchController)(Aricie.DNN.Modules.LuceneSearch.Common.Constants.ModuleNameLuceneSearchResults)
        UE.ResolveAndAddUpgrades()
        ' déterminer la version courante
        UE.MigrateSettings(New Version(tbVersion.Text), New Version(DMI.Version))

        WriteNewValueAndRedirect()
    End Sub

    Private Sub btnWriteDirectly_Click(sender As Object, e As System.EventArgs) Handles btnWriteDirectly.Click
         WriteNewValueAndRedirect()
    End Sub

    Private Sub WriteNewValueAndRedirect()
        Dim DMC As New DesktopModuleController()
        Dim DMI = DMC.GetDesktopModuleByModuleName(Aricie.DNN.Modules.LuceneSearch.Common.Constants.ModuleNameLuceneSearchResults)
        Dim ChangedValue = Global.UpgradeSystem.VersionManagement.InstalledVersion.CurrentlyInstalledVersion
        ChangedValue.Version = DMI.Version
        ChangedValue.InstalledDate = DateTime.Now
        Global.UpgradeSystem.VersionManagement.InstalledVersion.CurrentlyInstalledVersion = ChangedValue

        Response.Redirect(DotNetNuke.Common.Globals.NavigateURL())
    End Sub

End Class